package MemberController;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class MemberFindAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String sessionID  = null;
	private HttpSession session = null;
       
   
    public MemberFindAction() {
        super();
     
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("*****아아디/패스워드 찾기******");
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		RequestDispatcher view = null;
		PrintWriter out =response.getWriter();
		
		MembersBean mbean = new MembersBean();
		MembersDAO mdao = new MembersDAO();
		
		String action_code = request.getParameter("action_code");
		String numMode = request.getParameter("num");
		int start = numMode != null ? Integer.parseInt(numMode) : -1;
		session = request.getSession();
		if(action_code.equals("find")){
			switch(start){
				case 3:
					System.out.println("*****비밀번호 페이지 이동*****");
					request.setAttribute("check","4");
					session.setAttribute("up","0");	
					session.setAttribute("down","0");
					view = request.getRequestDispatcher("Main/FirstMain/Fm_Main.jsp");
					view.forward(request, response);
					break;
				case 1:
						System.out.println("*****아이디 페이지 이동*****");
						request.setAttribute("check","4");
						session.setAttribute("up","0");
						session.setAttribute("down","0");
						view = request.getRequestDispatcher("Main/FirstMain/Fm_Main.jsp");
						view.forward(request, response);
				break;
				
				case 2:
						System.out.println("*****아이디찾기이동*****");
						
						mbean.setMem_NameK(request.getParameter("Mem_NameK"));
						mbean.setMem_Email(request.getParameter("Mem_Email"));
						mbean.setMem_Year(request.getParameter("Mem_Year"));
						mbean.setMem_Month(request.getParameter("Mem_Month"));
						mbean.setMem_Day(request.getParameter("Mem_Day"));
						
						String Mem_Nick=mdao.findId(mbean);
						System.out.println(Mem_Nick);
						if(Mem_Nick != null){
							request.setAttribute("check","4");
							session.setAttribute("up","1");
							session.setAttribute("down","0");
							session.setAttribute("find","1");
							session.setAttribute("Mem_Nick",Mem_Nick);
							view = request.getRequestDispatcher("Main/FirstMain/Fm_Main.jsp");
							view.forward(request, response);
							break;
						}else
							request.setAttribute("check","4");
							session.setAttribute("up","1");
							session.setAttribute("down","0");
							session.setAttribute("find","2");
							view = request.getRequestDispatcher("Main/FirstMain/Fm_Main.jsp");
							view.forward(request, response);
							break;
			
				case 4:
					
						System.out.println("비밀번호 찾아보자");
						
						mbean.setMem_Nick(request.getParameter("Mem_Nick"));
						mbean.setMem_Question(request.getParameter("Mem_Question"));
						mbean.setMem_Answer(request.getParameter("Mem_Answer"));
						mbean.setMem_Email(request.getParameter("Mem_Email"));
						
						String Mem_Pass=mdao.findPass(mbean);
						System.out.println(Mem_Pass);
						
						if(Mem_Pass != null){
							request.setAttribute("check","4");
							session.setAttribute("up","0");
							session.setAttribute("down","1");
							session.setAttribute("find","1");
							session.setAttribute("Mem_Pass",Mem_Pass);
							view = request.getRequestDispatcher("Main/FirstMain/Fm_Main.jsp");
							view.forward(request, response);
							break;
						}else
							request.setAttribute("check","4");
							session.setAttribute("up","0");
							session.setAttribute("down","1");
							session.setAttribute("find","2");
							view = request.getRequestDispatcher("Main/FirstMain/Fm_Main.jsp");
							view.forward(request, response);
							break;
			}
		}
	}

}
